package com.ctshk.rpc.air.util;

import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.rpc.air.dto.*;
import com.ctshk.rpc.air.req.airStrategy.AirProductSaleStrategyDataInfoData;
import com.google.common.collect.Maps;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class AirProductUtil {


    /**
     * 转换 产品上架设置的加幅信息
     *
     * @param airStrategyDayInfoOffLineDTO
     */
    public static void assblemStrategyDesc(AirStrategyDayInfoOffLineDTO airStrategyDayInfoOffLineDTO, Integer soldNumber) {
        AirProductSaleStrategyDataInfoData airProductSaleStrategyDataInfoData = airStrategyDayInfoOffLineDTO.getAirProductSaleStrategyDataInfoData();
        if (Objects.isNull(airProductSaleStrategyDataInfoData)) return;
        if (NumberConstant.ONE.getValue().equals(airProductSaleStrategyDataInfoData.getAdjustType())) {
            airStrategyDayInfoOffLineDTO.setMarkString(airProductSaleStrategyDataInfoData.getAmount() + " HDK");
        } else {
            airStrategyDayInfoOffLineDTO.setMarkString(airProductSaleStrategyDataInfoData.getPercent() + " %");
        }
        airStrategyDayInfoOffLineDTO.setTripAmountString("去程:" + airProductSaleStrategyDataInfoData.getTripAmount() + " HDK");
        airStrategyDayInfoOffLineDTO.setReturnAmountSting("返程:" + airProductSaleStrategyDataInfoData.getReturnAmount() + " HDK");
        if (airProductSaleStrategyDataInfoData.getIsLimit().intValue() == 1) {
            airStrategyDayInfoOffLineDTO.setEveryDaySale(airProductSaleStrategyDataInfoData.getLimitNum() + "");
            if (null == soldNumber) {
                airStrategyDayInfoOffLineDTO.setToDaySold("0");
                airStrategyDayInfoOffLineDTO.setCanSale(airProductSaleStrategyDataInfoData.getLimitNum() + "");
            } else {
                airStrategyDayInfoOffLineDTO.setToDaySold(soldNumber.toString());
                int canSale = airProductSaleStrategyDataInfoData.getLimitNum().intValue() - soldNumber.intValue();
                airStrategyDayInfoOffLineDTO.setCanSale(canSale + "");
            }
        } else {
            airStrategyDayInfoOffLineDTO.setEveryDaySale("無限制");
            airStrategyDayInfoOffLineDTO.setCanSale("無限制");
            if (null == soldNumber) {
                airStrategyDayInfoOffLineDTO.setToDaySold("0");
            } else {
                airStrategyDayInfoOffLineDTO.setToDaySold(soldNumber.toString());
            }
        }
    }

    /**
     * 转换 产品上架设置的加幅信息
     *
     * @param airStrategyDayInfoOnLineDTO
     */
    public static void assblemStrategyDesc(AirStrategyDayInfoOnLineDTO airStrategyDayInfoOnLineDTO, Integer soldNumber) {
        AirProductSaleStrategyDataInfoData airProductSaleStrategyDataInfoData = airStrategyDayInfoOnLineDTO.getAirProductSaleStrategyDataInfoData();
        if (airProductSaleStrategyDataInfoData.getAdjustType().intValue() == NumberConstant.ONE.getValue().intValue()) {
            airStrategyDayInfoOnLineDTO.setMarkString(airProductSaleStrategyDataInfoData.getAmount() + " HDK");
        } else {
            airStrategyDayInfoOnLineDTO.setMarkString(airProductSaleStrategyDataInfoData.getPercent() + " %");
        }
        airStrategyDayInfoOnLineDTO.setTripAmountString("去程:" + airProductSaleStrategyDataInfoData.getTripAmount() + " HDK");
        airStrategyDayInfoOnLineDTO.setReturnAmountSting("返程:" + airProductSaleStrategyDataInfoData.getReturnAmount() + " HDK");
        if (airProductSaleStrategyDataInfoData.getIsLimit().intValue() == 1) {
            airStrategyDayInfoOnLineDTO.setEveryDaySale(airProductSaleStrategyDataInfoData.getLimitNum() + "");
            if (null == soldNumber) {
                airStrategyDayInfoOnLineDTO.setToDaySold("0");
                airStrategyDayInfoOnLineDTO.setCanSale(airProductSaleStrategyDataInfoData.getLimitNum() + "");
            } else {
                airStrategyDayInfoOnLineDTO.setToDaySold(soldNumber.toString());
                int canSale = airProductSaleStrategyDataInfoData.getLimitNum().intValue() - soldNumber.intValue();
                airStrategyDayInfoOnLineDTO.setCanSale(canSale + "");
            }
        } else {
            airStrategyDayInfoOnLineDTO.setEveryDaySale("無限制");
            airStrategyDayInfoOnLineDTO.setCanSale("無限制");
            if (null == soldNumber) {
                airStrategyDayInfoOnLineDTO.setToDaySold("0");
            } else {
                airStrategyDayInfoOnLineDTO.setToDaySold(soldNumber.toString());
            }
        }
    }

    /**
     * 组装产品成本加幅信息
     *
     * @param airProductPriceDTOList
     * @param airProductSaleStrategyPriceDTOList
     */
    public static List<AirProductPriceDTO> assmleProductStrategy(List<AirProductPriceDTO> airProductPriceDTOList,
                                                                 List<AirProductSaleStrategyPriceDTO> airProductSaleStrategyPriceDTOList,
                                                                 Map<String, Integer> airProductSaleRecordMap, Integer channelType, Long relationId) {
        Map<Long, AirProductSaleStrategyPriceDTO> saleStrategyPriceDTOMap = Maps.newHashMap();
        for (AirProductSaleStrategyPriceDTO airProductSaleStrategyPriceDTO : airProductSaleStrategyPriceDTOList) {
            saleStrategyPriceDTOMap.put(airProductSaleStrategyPriceDTO.getProductId(), airProductSaleStrategyPriceDTO);
        }
        for (AirProductPriceDTO airProductPriceDTO : airProductPriceDTOList) {
            AirProductSaleStrategyPriceDTO airProductSaleStrategyPriceDTO = saleStrategyPriceDTOMap.get(airProductPriceDTO.getProductId());
            if (airProductSaleStrategyPriceDTO.isForbidTheSale()) {
                airProductPriceDTO.setForbidTheSale(true);
                airProductPriceDTO.setAirProductPriceTouristDTOList(null);
            } else {
                List<AirProductSaleStrategyPriceDateDTO> airProductSaleStrategyPriceDateDTOList = airProductSaleStrategyPriceDTO.getAirProductSaleStrategyPriceDateDTOList();
                Map<LocalDate, AirProductSaleStrategyPriceDateDTO> priceDateDTOMap = Maps.newHashMap();
                for (AirProductSaleStrategyPriceDateDTO airProductSaleStrategyPriceDateDTO : airProductSaleStrategyPriceDateDTOList) {
                    priceDateDTOMap.put(airProductSaleStrategyPriceDateDTO.getDate(), airProductSaleStrategyPriceDateDTO);
                }
                List<AirProductPriceTouristDTO> airProductPriceTouristDTOList = airProductPriceDTO.getAirProductPriceTouristDTOList();
                for (AirProductPriceTouristDTO airProductPriceTouristDTO : airProductPriceTouristDTOList) {
                    List<AirProductPriceDateDTO> airProductPriceDateDTOList = airProductPriceTouristDTO.getAirProductPriceDateDTOList();
                    for (AirProductPriceDateDTO airProductPriceDateDTO : airProductPriceDateDTOList) {
                        AirProductSaleStrategyPriceDateDTO airProductSaleStrategyPriceDateDTO = priceDateDTOMap.get(airProductPriceDateDTO.getDate());
                        BigDecimal markPrice = airProductPriceDateDTO.getPrice();
                        if (airProductSaleStrategyPriceDateDTO.getAdjustType().intValue() == 1) {
                            markPrice = airProductPriceDateDTO.getPrice().add(new BigDecimal(airProductSaleStrategyPriceDateDTO.getValue()));
                        } else {
                            markPrice =
                                    airProductPriceDateDTO.getPrice().add((airProductPriceDateDTO.getPrice().multiply(new BigDecimal(airProductSaleStrategyPriceDateDTO.getValue()))).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
                        }
                        Integer dayStock = Optional.ofNullable(airProductPriceDateDTO.getDailyStock()).orElse(0);
                        if (airProductSaleStrategyPriceDateDTO.getIsLimit() == 1) {
                            dayStock = airProductSaleStrategyPriceDateDTO.getLimitNum();
                        }
                        String saleRecodeMapKey = getSaleRecodeMapKey(channelType, airProductPriceDateDTO.getDate(), relationId, airProductPriceDTO.getProductId());
                        Integer integer = Optional.ofNullable(airProductSaleRecordMap.get(saleRecodeMapKey)).orElse(0);
                        if (integer == null) {
                            integer = 0;
                        }
                        airProductPriceDateDTO.setDailyStock(dayStock - integer);
                        airProductPriceDateDTO.setPrice(markPrice);
                    }
                }
            }
        }
        return airProductPriceDTOList;
    }

    public static void validStrategyInfo(AirProductSaleStrategyDataInfoData airProductStrategyDataReq) {
        if (null == airProductStrategyDataReq) {
            throw new BusinessException(SystemError.AIR_PRODUCT_12011);
        }
        if (null == airProductStrategyDataReq.getTripAmount()) {
            throw new BusinessException(SystemError.AIR_PRODUCT_12014);
        }
        if (null == airProductStrategyDataReq.getReturnAmount()) {
            throw new BusinessException(SystemError.AIR_PRODUCT_12015);
        }
        if (null == airProductStrategyDataReq.getAdjustType()) {
            throw new BusinessException(SystemError.AIR_PRODUCT_12012);
        }
        if (airProductStrategyDataReq.getAdjustType().intValue() == 1) {
            if (null == airProductStrategyDataReq.getAmount() || airProductStrategyDataReq.getAmount().intValue() < 0) {
                throw new BusinessException(SystemError.AIR_PRODUCT_12013);
            }
        }
        if (airProductStrategyDataReq.getAdjustType().intValue() == 2) {
            if (null == airProductStrategyDataReq.getPercent() || airProductStrategyDataReq.getPercent().intValue() < 0) {
                throw new BusinessException(SystemError.AIR_PRODUCT_12013);
            }
        }
        if (null == airProductStrategyDataReq.getIsLimit()) {
            throw new BusinessException(SystemError.AIR_PRODUCT_12016);
        }
        if (airProductStrategyDataReq.getIsLimit().intValue() == 1) {
            if (null == airProductStrategyDataReq.getLimitNum() || airProductStrategyDataReq.getLimitNum().intValue() < 0) {
                throw new BusinessException(SystemError.AIR_PRODUCT_12017);
            }
        }
    }


    public static String getSaleRecodeMapKey(Integer channelType, LocalDate localDate, Long customId, Long productId) {
        StringBuffer mapKey = new StringBuffer();
        mapKey.append(productId + "_");
        mapKey.append(channelType + "_");
        mapKey.append(customId + "_");
        mapKey.append(localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        return mapKey.toString();
    }


//    public static void main(String[] args) {
//        LocalTime time = LocalTime.now();
//        //System.out.println(time); //21:05:42:764
//
//        LocalTime utcTime = LocalTime.now().plusHours(8).plusMinutes(44);
//        //System.out.println(utcTime);  //18:05:42:769
//
//        System.out.println(Duration.between(utcTime, time).getSeconds()/3600);  //2
//        System.out.println(Duration.between(time, utcTime).getSeconds()/3600);  //-3
//
//
//        LocalTime time0 = LocalTime.of(9,15),
//                time1 = LocalTime.of(10,35);
//        System.out.println(time0.until(time1, ChronoUnit.MINUTES));
//    }
}
